package com.bysj.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.bysj.dao.EvaluationDao;
import com.bysj.model.Evaluation;
import com.bysj.model.OrderEvaluation;
import com.bysj.model.User;
import com.bysj.utils.DBUtil;

public class EvaluationService {
	
	private EvaluationDao eDao = new EvaluationDao();
	
	public void addEvaluation(OrderEvaluation o,User user) {
		Connection con = null;
		List<Evaluation> evaluations = o.getEvaluations();
		for(int i=0;i<evaluations.size();i++) {
			try {
				con = DBUtil.getConnection();
				con.setAutoCommit(false);
				Evaluation evaluation = evaluations.get(i);
				evaluation.setCreatorId(user.getId());
				eDao.addEvaluation(con, evaluation);
				int id = eDao.getLastInsertId(con);
				con.commit();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				if (con != null)
					try {
						con.rollback();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
			}
		}
	}
	
	public void addReply(OrderEvaluation o,User user) {
		Connection con = null;
		List<Evaluation> evaluations = o.getEvaluations();
		for(int i=0;i<evaluations.size();i++) {
			try {
				con = DBUtil.getConnection();
				con.setAutoCommit(false);
				Evaluation evaluation = evaluations.get(i);
				evaluation.setCreatorId(user.getId());
				eDao.addReply(con, evaluation);
				int id = eDao.getLastInsertId(con);
				con.commit();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				if (con != null)
					try {
						con.rollback();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
			}
		}
	}
	public Evaluation getMyEvaluation(int orderId,int goodsId,int userId) {
		return eDao.getMyEvaluation(orderId,goodsId, userId);
	}
	
	public Evaluation getWindowshoperOrderEvaluation(int orderId,int goodsId) {
		return eDao.getWindowshoperOrderEvaluation(orderId,goodsId);
	}
	
	public List<Evaluation> getGoodeEvaluation(int goodsId) {
		return eDao.getGoodeEvaluation(goodsId);
	}
}
